package controllers;

import play.*;
import play.data.validation.Required;
import play.db.jpa.JPABase;
import play.libs.Mail;
import play.mvc.*;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;

import javax.mail.Session;

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

import models.*;

@With(Secure.class)
// this class is for the login, Doaa El-Hawary
public class Users extends Controller {

	public static void index() {
		render();
	}

	
	//Hellloo
	/**
	 * sending request.
	 * 
	 * @author wassim.gabriel
	 * @version 2011.05.03
	 * @category C3S14
	 * @param sender, receiver, type,content, date, saveAsDraft.
	 * @return returns a boolean indicating true if the request was sent.
	 * @throws EmailException
	 */
	
	public static boolean sendRequest(User sender, User receiver, String type,
			String content, boolean saveAsDraft) throws EmailException {
		Request request = new Request(sender, receiver, content,
				saveAsDraft, type);
		request.save();
		SimpleEmail email = new SimpleEmail();

		email.setFrom(sender.getEmail());
		email.setSubject(request.getType());
		email.setMsg(content);
		email.addTo(receiver.getEmail());
		Mail.send(email);
		return true;
	}

	/**
	 * sending topic request view.
	 * 
	 * @author wassim.gabriel
	 * @version 2011.05.03
	 * @category C3S14
	 * @param entityId
	 * @return void
	 */
	public static void requestTopicView(long entityId) {
		Eentity ent = Eentity.findById(entityId);
		System.out.println(entityId);
		long receiverId = -1;
		if (ent.getOrganizers() != null & ent.getOrganizers().size() != 0) {
			receiverId = ent.getOrganizers().get(0).getId();
		} else {
			receiverId = ent.getOwner().getId();
		}
		if (receiverId == -1) {
			return;
		}
		render(receiverId);
	}

	/**
	 * sending topic request.
	 * 
	 * @author wassim.gabriel
	 * @version 2011.05.03
	 * @category C3S14
	 * @param receiverId
	 *            , topicDescription, saveAsDraft.
	 * @return returns a boolean indicating true if the request was sent.
	 * @throws EmailException
	 */
	public static boolean sendTopicRequest(long receiverId,
			String topicDescription, boolean saveAsDraft) throws EmailException {
		if (saveAsDraft) {
			Requests.saveDraft(receiverId, topicDescription, saveAsDraft);
			return false;

		}// karim khalaf part.

		String name = session.get("username");
		User sender = (User) User.find("byUsername", name).fetch().get(0);
		User receiver = (User) User.find("byId", receiverId).fetch().get(0);
		if (!saveAsDraft) {
			if (sendRequest(sender, receiver, "Topic Request",
					topicDescription, saveAsDraft)) {
				String x = "you have succesfully sent request";
				render(x);
				return true;
			} else {

				String x = "there is a problem in sending request please try again later";
				render(x);
				return false;
			}

		}
		return false;
	}
	/**
	 * notify organizer of an idea being reported as spam and the creators of
	 * this idea
	 * 
	 * @author Caroline Tawfik
	 * @version 2011.0501
	 * @category C3S8
	 * @params long IdeaId: idea reported
	 * @return nothing
	 * @throws EmailException
	 * 
	 **/

	public static void notifySpammedIdea(long ideaId)

	throws EmailException {
		Idea idea = Idea.findById(ideaId);
		String username = session.get("username");
		User reporter = (User) User.find("byUsername", username).fetch().get(0);
		// User reporter = User.findById(userId);
		LinkedList<User> organizers = idea.getEntity().getOrganizers();
		 LinkedList<User> ideaCreator = idea.getCreators();
		for (int i = 0; i < organizers.size(); i++) {
			String subject = reporter.getfName() + " " + reporter.getlName()
					+ " has reported your idea as spam.";
			String content = reporter.getfName() + " " + reporter.getlName()
					+ " has reported your idea:\n" + idea.getTitle()
					+ "\non the topic:" + idea.getTopic().getTitle()
					+ "\nas spam.";
			reporter.sendNotification(organizers.get(i), subject, content);

		}
		for (int i = 0; i < ideaCreator.size(); i++) {
			String subject = reporter.getfName() + " " + reporter.getlName()
					+ " has reported your idea as spam.";
			String content = reporter.getfName() + " " + reporter.getlName()
					+ " has reported your idea:\n" + idea.getTitle()
					+ "\non the topic:" + idea.getTopic().getTitle()
					+ "\nas spam.";
			reporter.sendNotification(ideaCreator.get(i), subject, content);

		}
	}

	/**
	 * notify organizer of a topic is being reported as spam and the creators of
	 * this topic
	 * 
	 * @author nora zakhary
	 * @version 2011.0504
	 * @category C3S7
	 * @param long topicId
	 * 
	 * @return nothing
	 * @throws EmailException
	 * 
	 **//*
	public static void notifySpammedTopic(long topicId)

	throws EmailException {
		Topic topic = Topic.findById(topicId);
		String username = session.get("username");
		User reporter = (User) User.find("byUsername", username).fetch().get(0);
		// User reporter = User.findById(userId);
		LinkedList<User> organizers = topic.getEntity().getOrganizers();
		// LinkedList ideaCreator = idea.getCreators();
		for (int i = 0; i < organizers.size(); i++) {
			String subject = reporter.getfName() + " " + reporter.getlName()
					+ " has reported your topic as spam.";
			String content = reporter.getfName() + " " + reporter.getlName()
					+ " has reported your topic:\n" + topic.getTitle()
					+ "\nas spam.";
			reporter.sendNotification(organizers.get(i), subject, content);

		}
	}
*/
	/**
	 * User following a specific tag
	 * 
	 * @author Mostafa Badr
	 * @version 2010.0426
	 * @category C2S16
	 * @params User: The user whose permission is being checked
	 * @return boolean: whether or not the user is already following this tag
	 */
	public static boolean followTag(long tagId, long userId) {
		Tag tag = Tag.findById(tagId);
		User user = Tag.findById(userId);
		String result = "";
		LinkedList<User> test = tag.getFollowers();
		if (test.contains(user)) {
			result = " You are already following this tag ";
			render(result);
			return false;
		}
		tag.setFollowers(user);
		result = " You are now following this tag ";
		render(tag.getId(), result);
		return true;
		
	}

	/**
	 * User following a specific entity
	 * 
	 * @author Mostafa Badr
	 * @version 2010.0428
	 * @category C2S17
	 * @params : the user and the entity which the user want to follow
	 * @return boolean: whether or not the user is already following this entity
	 */

	public static boolean followEntity(Eentity entityId, User userId) {
		Eentity entity = Eentity.findById(entityId);
		User user = User.findById(userId);
		String result = "";
		
		
		LinkedList<User> test = entity.getFollowers();
		if (test.contains(user)) {
			result = " You are already following this entity ";
			render(result);
			return false;
		}
		entity.setFollowers(user);
		result = " You are now following this entity ";
		render(entityId.getId(), result);
		return true;
	}

	/**
	 * Organizer creates a new plan, this is what's sent to
	 * the html file
	 * 
	 * @author Omar Hassab
	 * @version 2011.0506
	 * @category C5S8
	 * @param nothing
	 * @return nothing
	 */
	public static void createPlanView() {
		User user = (User.find("byUsername", Security.connected()).first());
		System.out.println(user.getlName());
		long orgID = user.getId();
		render(orgID);
	}
	
	/**
	 * Organizer creates a new plan for executing one or more ideas and saves it
	 * to the database
	 * 
	 * @author Omar Hassab
	 * @version 2011.0429
	 * @category C5S1
	 * @param organizerID
	 *            : creator of the plan, name: name of the new plan, status:
	 *            status of the new plan, theRequirements: requirements of the
	 *            new plan, theIdeas: list of ideas to be executed
	 * @return nothing
	 */
	public static void createPlan(long organizerID, String name, String status,
			String theRequirements, String theIdeas) {
		User organizer = User.findById(organizerID);
		LinkedList<String> requirements = formRequirementsList(theRequirements);
		boolean theStatus = false;
		List<Idea> ideas = formIdeasList(theIdeas);
		for (int j = 1; j < ideas.size(); j++) {
			if (!(ideas.get(j).getOrganization().getId() == ideas.get(0)
					.getOrganization().getId())) {
				String x = "Ideas should belong to the same organization";
				render(x);
			}
		}
		for (int i = 0; i < organizer.getRoleList().size(); i++) {
			if (organizer.getRoleList().get(i).getTitle()
					.equalsIgnoreCase("Organizer")) {
				if (ideas != null) {
					List<Plan> plans = Plan.findAll();
					if (status.equalsIgnoreCase("active"))
						theStatus = true;
					Plan plan = new Plan(name, theStatus, requirements, ideas);

					if (plans.contains(plan)) {
						String x = "plan is already created";
						render(x);
					}
					plan.setOrganization(ideas.get(0).getOrganization());
					plan.getUsers().add(organizer);
					plan.save();
					String x = "you have successfully saved the plan";
					render(x);
				}
			}
		}
		String x = "plan can't be created";
		render(x);
	}

	/**
	 * Helper method for the method createPlan, it takes a String of all
	 * requirements and returns them in a list.
	 * 
	 * @author Omar Hassab
	 * @version 2011.0506
	 * @category C5S1
	 * @param theRequirements
	 *            : String of all requirements of the new plan
	 * @return requirements: List of all requirements
	 */
	public static LinkedList<String> formRequirementsList(String theRequirements) {
		LinkedList<String> requirements = new LinkedList();
		if (!theRequirements.isEmpty()) {
			String[] arrayOfRequirements = theRequirements.split("/");
			for (int i = 0; i < arrayOfRequirements.length; i++)
				requirements.add(arrayOfRequirements[i]);
		}
		return requirements;
	}

	/**
	 * Helper method for the method createPlan, it takes a String of all ideas
	 * and returns them in a list.
	 * 
	 * @author Omar Hassab
	 * @version 2011.0506
	 * @category C5S1
	 * @param theIdeas
	 *            : String of all Ideas of the new plan
	 * @return ideas: List of all ideas
	 */
	public static List<Idea> formIdeasList(String theIdeas) {
		List<Idea> ideas = new LinkedList();
		List<Idea> allIdeas = Idea.findAll();
		String[] arrayOfIdeas = theIdeas.split("/");
		for (int i = 0; i < arrayOfIdeas.length; i++) {
			for (int j = 0; j < allIdeas.size(); j++) {
				if (allIdeas.get(j).getTitle()
						.equalsIgnoreCase(arrayOfIdeas[i])
						&& allIdeas.get(j).isExecutionStatus())
					ideas.add(allIdeas.get(j));
			}
		}
		return ideas;
	}

	/**
	 * Organizer responds to a user volunteer request, this is what's sent to
	 * the html file
	 * 
	 * @author Omar Hassab
	 * @version 2011.0506
	 * @category C5S8
	 * @param nothing
	 * @return nothing
	 */
	public static void respondToVolunteerRequestView() {
		User user = (User.find("byUsername", Security.connected()).first());
		System.out.println(user.getlName());
		long orgID = user.getId();
		render(orgID);
	}

	/**
	 * Organizer responds to a user volunteer request
	 * 
	 * @author Omar Hassab
	 * @version 2011.0430
	 * @category C5S8
	 * @param organizerID
	 *            : organizer that will respond to the request, response: the
	 *            response the organizer will respond with
	 * @return nothing
	 */
	public static void respondToVolunteerRequest(long organizerID,
			String response) {
		User organizer = User.findById(organizerID);
		for (int i = 0; i < organizer.getRoleList().size(); i++) {
			if (organizer.getRoleList().get(i).getTitle()
					.equalsIgnoreCase("Organizer")) {
				List<Request> requests = Request.findAll();
				for (int k = 0; k < requests.size(); k++) {
					if ((requests.get(k).getReciever() == organizer)
							&& requests.get(k).getType()
									.equalsIgnoreCase("action plan request")) {
						requests.get(k).setResponse(response);
						String x = "Respond sent";
						render(x);
					}
				}
			}
		}
		String x = "You can't respond to the request now";
		render(x);
	}

	/**
	 * 
	 * User following a specific Organization
	 * 
	 * @author Ibrahim Kessba
	 * @version 2010.0429
	 * @category C2S14
	 * @params User userFollower: User Requesting to Joing the Organization ,
	 *         Organization organizationToBeFollowed: Organization To Be
	 *         Followed By the User
	 * @return True: User Succesfully Added to Organization followers List
	 *         ,False: User Already Following the Orgainzation
	 */
	public static boolean followOrganization(
			Organization organizationToBeFollowed, User userFollower) {

		LinkedList organizationFollowerTemp = organizationToBeFollowed
				.getFollowers();

		if (organizationFollowerTemp.contains(userFollower)) {

			System.out.println("User Name : " + userFollower
					+ "User is Already Following " + organizationToBeFollowed
					+ " Organization");
			return false; // Already in Followers List
		}

		// organizationFollowerTemp.add(userFollower);

		// organizationToBeFollowed.setFollowers(organizationFollowerTemp);
		organizationToBeFollowed.setFollowers(userFollower);
		System.out.println("User Name : " + userFollower + "is Now Following  "
				+ organizationToBeFollowed + " Organization");

		return true;
	}

	/**
	 * User following a specific Topic
	 * 
	 * @author Ibrahim Kessba
	 * @version 2010.0429
	 * 
	 * 
	 * @category C2S15
	 * @params User: Follower , Topic: To be Followed
	 * @return boolean: Followed / Already is Following This Topic
	 */
	public static boolean followTopic(Topic topicToBeFollowed, User userFollower) {

		LinkedList topicFollowerListTemp = topicToBeFollowed.getFollowers();

		if (topicFollowerListTemp.contains(userFollower)) {
			System.out.println("User Name : " + userFollower
					+ "User is Already Following " + topicToBeFollowed
					+ " Topic");
			return false; // Already in Followers List
		}
		topicFollowerListTemp.add(userFollower);
		System.out.println("User Name : " + userFollower + "is Now Following  "
				+ topicToBeFollowed + " Topic");

		// topicToBeFollowed.setFollowers(topicFollowerListTemp);
		return true;
	}

	/**
	 * view to invite unregistered user to entity
	 * 
	 * @author Tarek Sheasha
	 * @param entityId
	 */
	public static void inviteUser(long entityId) {
		Eentity entity = Eentity.findById(entityId);
		User inviter = User.findByUsername(Security.connected());
		for (int i = 0; i < inviter.getRoleList().size(); i++) {
			if (inviter.getRoleList().get(i).getTitle()
							.equalsIgnoreCase("Lead"))
				render(entityId);
		}
		notAllowed();
	}

	/**
	 * view for inviting user to organization with full leads priviliges
	 * 
	 * @author Tarek Sheasha
	 * @param orgId
	 */
	public static void inviteRegUser(long orgId) {
		List user = User.all().fetch();
		List newUsers = new LinkedList<User>();
		for (int i = 0; i < user.size(); i++) {
			if(!((User) user.get(i)).isDeleted()) {
				newUsers.add(user.get(i));
			}
		}
		Organization organization = Organization.findById(orgId);
		User inviter = User.findByUsername(Security.connected());
		for (int i = 0; i < inviter.getRoleList().size(); i++) {
			if (inviter.getRoleList().get(i).getTitle()
							.equalsIgnoreCase("Lead"))
				render(orgId, newUsers);
		}
		notAllowed();
	}

	/**
	 * view for a user to go to once they are not qualified to pass condition of
	 * being with a certain role
	 * 
	 * @author Tarek Sheasha
	 */
	public static void notAllowed() {
		render();
	}

	/**
	 * accepting invitation to organisation with full lead's privileges
	 * 
	 * @author Tarek Sheasha
	 * @version 26.04.2011
	 * @category C1S8
	 * @param invitationId
	 * @throws EmailException
	 */
	public static void acceptInviteFullPrivileges(long invitationId)
			throws EmailException {
		Invitation invitation = Invitation.findById(invitationId);
		invitation.getReceiver().acceptInvitationOrganization(invitation);

		invitation.save();

	}

	/**
	 * rejecting invitation to organisation with full lead's privileges
	 * 
	 * @author Tarek Sheasha
	 * @version 26.04.2011
	 * @category C1S8
	 * @param invitationId
	 * @throws EmailException
	 */
	public static void rejectInviteFullPrivileges(long invitationId)
			throws EmailException {
		Invitation invitation = Invitation.findById(invitationId);
		invitation.getReceiver().rejectInvitationOrganization(invitation);

		invitation.save();
	}

	/**
	 * accepting invitation to Entity as unregistered user
	 * 
	 * @author Tarek Sheasha
	 * @version 26.04.2011
	 * @category C1S5
	 * @param invitationId
	 * @throws EmailException
	 */
	public static void acceptUnregInviteEntity(long invitationId)
			throws EmailException {
		Invitation invitation = Invitation.findById(invitationId);
		invitation.getReceiver().acceptUnregInvitationEntity(invitation);

		invitation.save();

	}

	/**
	 * rejecting invitation to Entity as unregistered user
	 * 
	 * @author Tarek Sheasha
	 * @version 26.04.2011
	 * @category C1S5
	 * @param invitationId
	 * @throws EmailException
	 */
	public static void rejectUnredInviteEntity(long invitationId)
			throws EmailException {
		Invitation invitation = Invitation.findById(invitationId);
		invitation.getReceiver().rejectUnregInvitationEntity(invitation);

		invitation.save();

	}

	/**
	 * Invites a user to join an organization with a specific entity assigned to
	 * him/her
	 * 
	 * @author Doaa El-Hawary
	 * @version 2011.0427
	 * @category C1S7
	 * @param toBeInvited
	 *            : Invited user
	 * @param Eentity
	 *            : the entity that should be assigned to the invited user one
	 *            they accept
	 * @param sender
	 *            : the organization lead who sends the invitation
	 * @return void because it just sends an invitation.
	 * @throws EmailException
	 */
	public static void inviteToEntity(String toBeInvitedUsername, String date,
			long entityId) throws EmailException {
		User sender = User.findByUsername(Security.connected());
		User toBeInvited = User.findByUsername(toBeInvitedUsername);
		Eentity entity = Eentity.findById(entityId);
		String roleOfSender = sender.getUserType(entity.getOrganization());
		if (roleOfSender.equalsIgnoreCase("Lead")) {
			if (toBeInvited.getEntityList().contains(entity)) {
				return;
			}
			if (entity.getMembers().contains(toBeInvited)) {
				String invitationContent = "You're invited to join this entity: "
						+ entity.getName();
				Invitation invitation = new Invitation(sender, toBeInvited,		//default role entity??
						
						invitationContent, entity);
				toBeInvited.getReceivedInvitations().add(invitation);
				sender.sendNotification(toBeInvited, "Inivitation",
						invitationContent);
				invitation.save();
				render("Success");
			}
		}
	}
	public static void inviteToEntityHelper(long entityId) {
		render(entityId);
	}

	/**
	 * choosing 1 idea and eliminating the other one after the organizer chooses 1 in another method
	 * 
	 * @author Aly Mostafa
	 * @version 2011.04.29
	 * @category C3S17
	 * @param the two id's to be merged
	 * @return void
	 */

	public static void mergeIdeas1(long id1, long id2) {
		// Idea idea1 = Idea.findById(id1);
		Idea idea2 = Idea.findById(id2);
		String name = Security.connected();
		// User user = (User) User.find("username", name).fetch().get(0);

		User user = (User) User.findByUsername(name);
		if (idea2.getTopic() != null) {
			if (idea2.getTopic().getInitiator() == user)

	
			idea2.delete();

			render();
		} else{

		if (idea2.getEntity() != null) {
			if (idea2.getEntity().getOrganizers().contains(user))
				idea2.delete();

			render();
		} else {
			if (idea2.getOrganization().getOrganizers().contains(user)
					|| idea2.getOrganization().getLead() == user) {
				idea2.delete();
				render();
			}
		}

	}
		}

	/**
	 * view of user
	 * 
	 * @author Tarek Sheasha
	 * @param currentUser
	 */
	public static void viewUser(long userId) {
		User currentUser = User.findById(userId);
		render(currentUser);
	}

	/**
	 * view of users in an organizaiton
	 * 
	 * @author Tarek Sheasha
	 * @param orgId
	 */
	public static void viewUsersInOrg(long orgId) {
		Organization org = Organization.findById(orgId);
		List users = org.getAllUsersInOrganization();
		render(users);

	}

	/**
	 * views of users in an entity
	 * 
	 * @author Tarek Sheasha
	 * @param entityId
	 */
	public static void viewUsersInEntity(long entityId) {
		Eentity entity = Eentity.findById(entityId);
		List users = entity.getAllUsersInEntity();
		render(users);

	}

	/**
	 * @author Tarek Sheasha
	 * @param userId
	 */
	public static void deleteUser(long userId) {
		User toBeDeleted = User.findById(userId);
		User admin = User.find("byUsername", Security.connected()).first();
		admin.deleteFromSystem(toBeDeleted);
		viewAllUsers();
	}

	/**
	 * view all users in system
	 * 
	 * @author Tarek Sheasha
	 */
	public static void viewAllUsers() {
		List user = User.all().fetch();
		List newUsers = new LinkedList<User>();
		for (int i = 0; i < user.size(); i++) {
			if(!((User) user.get(i)).isDeleted()) {
				newUsers.add(user.get(i));
			}
		}
		render(newUsers);
	}


	/**
	 * The Oganizer Sets the idea and Sets The creator of the new Merged Idea which will be both creators
	 * 
	 * @author Aly Mostafa
	 * @version 2011.04.29
	 * @category C3S17
	 * @param ids of two ideas,String title,
			String details, long belonger(id of what it belongs to), String type of what it belongs to
	 * @return void
	 */

	public static void mergeIdeas2(long id1, long id2, String title,
			String details, long belonger, String var) {

		Idea idea1 = Idea.findById(id1);
		Idea idea2 = Idea.findById(id2);

		LinkedList<User> idea1User = idea1.getCreators();
		LinkedList<User> idea2User = idea2.getCreators();

		LinkedList<User> total = new LinkedList<User>();
		total.addAll(idea1User);
		total.addAll(idea2User);

		String name = Security.connected();
		User user = (User) User.findByUsername(name);

		Idea i = null;
		System.out.println(var + "x");
		if (var.equalsIgnoreCase("topic")) {
			Topic t = idea1.getTopic();
			System.out.println(t.getTitle());

			if (t.getInitiator() == user
					|| idea1.getOrganization().getLead() == user)
				i = new Idea(title, details, false, t, total);
			else
				error("You are not Authorised");

		} else{ 
			if (var.equalsIgnoreCase("entity")) {
			Eentity e = idea1.getEntity();
			if (e.getOrganizers().contains(user)
					|| idea1.getOrganization().getLead() == user)
				i = new Idea(title, details, false, e, total);
			else
				error("You are not Authorised");	
			
		} else {
			Organization o = idea1.getOrganization();
			if (o.getOrganizers().contains(user)
					|| idea1.getOrganization().getLead() == user)
				i = new Idea(title, details, false, o, total);
			else
				error("You are not Authorised");
		}
		}

		idea1.delete();
		idea2.delete();
		//i.save();
	}

	/**
	 * Responds to invitations for registered users
	 * 
	 * @author Doaa El-Hawary
	 * @version 2011.0505
	 * @category C1S7
	 * @param InvitationId
	 *            : the invitation that has a response
	 * @param String response
	 *            : the response of the user, "Accept"/"Reject"
	 * @param String
	 *            date: the date of responding to the invitation
	 * @return void
	 * @throws EmailException
	 */
	public static void regUsersRespondToInvitation(Long invitationId, String response, String date) throws EmailException {
		Invitation tempInv = Invitation.findById(invitationId);
		User receiver = tempInv.getReceiver();
		receiver.respondToInvitation(invitationId, response, date);
		render("Success");
	}
	public static void respondHelper(long invitationId) {
		render(invitationId);
	}

	/**
	 * increments user's pointCounter by a larger value when his idea is
	 * executed
	 * 
	 * @author Caroline Tawfik
	 * @version 2011.05.01
	 * @category C3S12
	 * @params user: the ID user whose idea was executed to increment his
	 *         pointCounter
	 * @return void
	 */
	public static void incrementExecutionCounter(LinkedList<User> creators) {
		for (int i = 0; i < creators.size(); i++) {
			creators.get(i).setPointCounter(
					creators.get(i).getPointCounter() + 10);
			creators.get(i).save();
		}
	}

	/**
	 * The Organizer closes idea and promote it for execution
	 * 
	 * @author Aly Mostafa
	 * @version 2011.2904
	 * @category C3S6
	 * @param Idea id 
	 * @return void
	 */

	public static void closeAndPromote(long ideaId) throws EmailException {
		String username = session.get("username");
		User user = (User) User.findByUsername(username);
		Idea idea = Idea.findById(ideaId);
		Eentity entity = idea.getEntity();
		Topic topic = idea.getTopic();
		if (entity.getOrganizers().contains(user)) {
			topic.setPromoted(true);
			idea.setActivityStatus(true);
			incrementExecutionCounter(idea.getCreators());
			topic.setStatus("closed");

		} else {

		}

	}

	/**
	 * View for Merging ideas
	 * 
	 * @author Aly Mostafa
	 * @version 2011.04.30
	 * @category C3S17
	 * @return void
	 */

	// public void mergeView() {
	// render();
	// }

	/**
	 * Organization Lead Responding To Organization Requests : Approve Or Reject
	 * 
	 * @author Ibrahim Kessba
	 * @version 2011.0502
	 * @category C2S8
	 * @params Organization: Organization having Joining Requestes , User:
	 *         Organiztional Lead To Respond to These Requestes
	 * @return void
	 */
	public static void respondOrganizationRequests(
			Organization organizationTemp, User organizationLead) {

		// List<Request> requests = organizationTemp.getRequests();

	}

	/**
	 * View to show organizers in an entity
	 * 
	 * @author Hossam Saraya
	 * @version May 2, 2011
	 * @category C1S10
	 * @params Entity Id
	 */
	public static void viewOrganizers(Long entityId) {
		Eentity entity = Eentity.findById(entityId);

		LinkedList<User> organizers = entity.getOrganizers();

		System.out.print(organizers.size());

		render(organizers);
	}

	public static List getAllTopics(int userID) {
		User x = User.findById(userID);
		return (x.getTopicList());

	}

	public static List getAllEntities(int userID) {
		User x = User.findById(userID);
		return (x.getEntityList());

	}

	public static List getAllTags(int userID) {
		User x = User.findById(userID);
		return (x.getTagList());

	}

}
